<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>tomcat 的优化配置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>tomcat 的优化配置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<h2 id="一、精简Tomcat和配置文件"><a href="#一、精简Tomcat和配置文件" class="headerlink" title="一、精简Tomcat和配置文件"></a>一、精简Tomcat和配置文件</h2><p>1．删除不需要的管理应用和帮助应用，提高tomcat安全性。</p>
<ul>
<li>删除webapps下所有文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm –fr <span class="variable">$CATALINA_HOME</span>/webapps/*</div></pre></td></tr></table></figure>
<ul>
<li>删除server/wenapps下所有文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm –fr <span class="variable">$CATALINA_HOME</span>/server/webapps/*</div></pre></td></tr></table></figure>
<p>2．精简sever.xml配置文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用tomcat发布版本中的最小配置文件，提高性能，如果有功能上的需求，在逐个的加入功能配置。</p>
<ul>
<li>备份原来的server.xml为server.xml_bak</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv server.xml server.xml_bak</div></pre></td></tr></table></figure>
<ul>
<li>复制server-minimal.xml为server.xml</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp server-minimal.xml server.xml</div></pre></td></tr></table></figure>
<h2 id="二、连接器优化"><a href="#二、连接器优化" class="headerlink" title="二、连接器优化"></a>二、连接器优化</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在$CATALINA_HOME/conf/server.xml配置文件中的Connetctor节点，和连接数相关的参数配置和优化。</p>
<h3 id="maxThreads"><a href="#maxThreads" class="headerlink" title="maxThreads"></a>maxThreads</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。默认值200。 可以根据机器的时期性能和内存大小调整，一般可以在400-500。最大可以在800左右。</p>
<h3 id="acceptCount"><a href="#acceptCount" class="headerlink" title="acceptCount"></a>acceptCount</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定当所有可以使用的处理请求的线程数都被使用时，可以放到处理队列中的请求数，超过这个数的请求将不予处理。默认值10。 </p>
<h3 id="minSpareThreads"><a href="#minSpareThreads" class="headerlink" title="minSpareThreads"></a>minSpareThreads</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat初始化时创建的线程数。默认值4。 </p>
<h3 id="maxSpareThreads"><a href="#maxSpareThreads" class="headerlink" title="maxSpareThreads"></a>maxSpareThreads</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一旦创建的线程超过这个值，Tomcat就会关闭不再需要的socket线程。默认值50。 </p>
<h3 id="enableLookups"><a href="#enableLookups" class="headerlink" title="enableLookups"></a>enableLookups</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;是否反查域名，默认值为true。为了提高处理能力，应设置为false </p>
<h3 id="connnectionTimeout"><a href="#connnectionTimeout" class="headerlink" title="connnectionTimeout"></a>connnectionTimeout</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;网络连接超时，默认值20000，单位：毫秒。设置为0表示永不超时，这样设置有隐患的。通常可设置为30000毫秒。(本系统由于与后台系统接口超时时间较长，使用设置为60000) </p>
<h3 id="maxKeepAliveRequests"><a href="#maxKeepAliveRequests" class="headerlink" title="maxKeepAliveRequests"></a>maxKeepAliveRequests</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保持请求数量，默认值100。 </p>
<h3 id="bufferSize"><a href="#bufferSize" class="headerlink" title="bufferSize"></a>bufferSize</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输入流缓冲大小，默认值2048 bytes。 </p>
<h3 id="compression"><a href="#compression" class="headerlink" title="compression"></a>compression</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩传输，取值on/off/force，默认值off。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中和最大连接数相关的参数为maxThreads和acceptCount。如果要加大并发连接数，应同时加大这两个参数。web server允许的最大连接数还受制于操作系统的内核参数设置，通常Windows是2000个左右，Linux是1000个左右。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat中如何禁止和允许列目录下的文件 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在$CATALINA_HOME/conf/web.xml中，把listings参数设置成false即可，如下： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">listings</div><div class="line"><span class="literal">false</span></div><div class="line">...</div></pre></td></tr></table></figure>
<h3 id="具体操作"><a href="#具体操作" class="headerlink" title="具体操作"></a>具体操作</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi <span class="variable">$CATALINA_HOME</span>/conf/server.xml</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改用于AJP的连接：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8009"</span> protocol=<span class="string">"AJP/1.3"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8009"</span></div><div class="line">               maxTreads=<span class="string">"500"</span> minSpareThreads=<span class="string">"10"</span> maxSpareThreads=<span class="string">"50"</span></div><div class="line">               acceptCount=<span class="string">"50"</span> connectionTimeout=<span class="string">"60000"</span></div><div class="line">               enableLookups=<span class="string">"false"</span> redirectPort=<span class="string">"8443"</span> protocol=<span class="string">"AJP/1.3"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改通用连接：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8080"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8080"</span></div><div class="line">               maxTreads=<span class="string">"500"</span> minSpareThreads=<span class="string">"10"</span> maxSpareThreads=<span class="string">"50"</span></div><div class="line">               acceptCount=<span class="string">"50"</span> connectionTimeout=<span class="string">"60000"</span></div><div class="line">               enableLookups=<span class="string">"false"</span> redirectPort=<span class="string">"8443"</span> protocol=<span class="string">"AJP/1.3"</span></div><div class="line">           compression=<span class="string">"on"</span></div><div class="line">           compressionMinSize=<span class="string">"2048"</span></div><div class="line">           noCompressionUserAgents=<span class="string">"gozilla, traviata"</span></div><div class="line">           compressableMimeType=<span class="string">"text/html,text/xml"</span>/&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改主机和应用配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Host name=<span class="string">"localhost"</span> appBase=<span class="string">"webapps"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;Host name=<span class="string">"localhost"</span> appBase=<span class="string">" "</span></div><div class="line">       unpackWARs=<span class="string">"true"</span> autoDeploy=<span class="string">"true"</span></div><div class="line">       xmlValidation=<span class="string">"false"</span> xmlNamespaceAware=<span class="string">"false"</span>&gt;</div><div class="line">        &lt;Context path=<span class="string">""</span> docBase=<span class="string">"/www/xxxx/site/web"</span> reloadable=<span class="string">"true"</span> debug=<span class="string">"0"</span>/&gt;</div><div class="line">      &lt;/Host&gt;</div></pre></td></tr></table></figure>
<h2 id="三、优化JDK"><a href="#三、优化JDK" class="headerlink" title="三、优化JDK"></a>三、优化JDK</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat默认可以使用的内存为128MB,Windows下,在文件{tomcat_home}/bin/catalina.bat，Unix下，在文件$CATALINA_HOME/bin/catalina.sh的前面，增加如下设置： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">JAVA_OPTS=<span class="string">'$JAVA_OPTS -Xms[初始化内存大小] -Xmx[可以使用的最大内存]</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或设置环境变量：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_OPTS=”<span class="variable">$JAVA_OPTS</span> -Xms[初始化内存大小] -Xmx[可以使用的最大内存]”</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一般说来，你应该使用物理内存的 80% 作为堆大小。如果本机上有Apache服务器，可以先折算Apache需要的内存，然后修改堆大小。建议设置为70％；建议设置[[初始化内存大小]等于[可以使用的最大内存]，这样可以减少平凡分配堆而降低性能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本例使用加入环境变量的方式：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi /etc/profile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_OPTS=”<span class="variable">$JAVA_OPTS</span> -Xms700 –Xmx700</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span> /etc/profile</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Tomcat/8. tomcat 的优化配置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
